import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '管理首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  // {
  //   path: '/guide',
  //   component: Layout,
  //   redirect: '/guide/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/guide/index'),
  //       name: 'Guide',
  //       meta: { title: 'Guide', icon: 'guide', noCache: true }
  //     }
  //   ]
  // },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/member',
    component: Layout,
    redirect: '/member/member',
    alwaysShow: true, // will always show the root menu
    name: 'member',
    meta: {
      title: '论坛会员',
      icon: 'user',
      roles: ['Admin', 'Editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'member',
        component: () => import('@/views/member/member'),
        name: 'MemberList',
        meta: {
          title: '会员管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'order',
        component: () => import('@/views/member/order'),
        name: 'OrderList',
        meta: {
          title: '订单管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'type',
        component: () => import('@/views/member/type'),
        name: 'MemberType',
        meta: {
          title: '分类管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      }
    ]
  },
  {
    path: '/ware',
    component: Layout,
    redirect: '/ware/ware',
    alwaysShow: true, // will always show the root menu
    name: 'ware',
    meta: {
      title: '论坛商品',
      icon: 'edit',
      roles: ['Admin', 'Editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'ware',
        component: () => import('@/views/ware/ware'),
        name: 'WareList',
        meta: {
          title: '商品管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'order',
        component: () => import('@/views/ware/order'),
        name: 'OrderList',
        meta: {
          title: '订单管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'inventory',
        component: () => import('@/views/ware/inventory'),
        name: 'InventoryList',
        meta: {
          title: '库存管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'type',
        component: () => import('@/views/ware/type'),
        name: 'TypeList',
        meta: {
          title: '分类管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      }
    ]
  },
  {
    path: '/article',
    component: Layout,
    redirect: '/article/article',
    alwaysShow: true, // will always show the root menu
    name: 'article',
    meta: {
      title: '论坛帖子',
      icon: 'form',
      roles: ['Admin', 'Editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'article',
        component: () => import('@/views/article/article'),
        name: 'ArticleList',
        meta: {
          title: '帖子管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'comment',
        component: () => import('@/views/article/comment'),
        name: 'CommentList',
        meta: {
          title: '评论管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'type',
        component: () => import('@/views/article/type'),
        name: 'TypeList',
        meta: {
          title: '分类管理',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/user',
    alwaysShow: true, // will always show the root menu
    name: 'user',
    meta: {
      title: '用户管理',
      icon: 'tree',
      roles: ['Admin', 'Editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/user/user'),
        name: 'UserList',
        meta: {
          title: '用户',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      },
      {
        path: 'userType',
        component: () => import('@/views/user/userType'),
        name: 'UserTypeList',
        meta: {
          title: '用户类型',
          roles: ['Admin', 'Editor'] // you can set roles in root nav
        }
      }
    ]
  },

  /** when your routing map is too long, you can split it into small modules **/

  {
    path: 'appLink',
    component: Layout,
    children: [
      {
        path: 'http://uni.testdemo.ltd',
        meta: { title: 'APP端H5', icon: 'link' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  // 404 页面必须放在最后
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
